﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BAL
{
    public partial class ICEntrustDetail : Form
    {
        private LoginService.BarcodeJudgeServices bjs = new BAL.LoginService.BarcodeJudgeServices();
        //Socket cliSocket = null;
        //string ipAddr = "192.168.1.211";
        //int port = 1024;

        private string FBrNo;
        private string FEntryNo;
        private string PkgsBarCode;
        private string PlaceBarCode;
        private string PlaceName;
        private string user = Program.user;
        private string ID;

        public ICEntrustDetail()
        {
            InitializeComponent();
        }

        public ICEntrustDetail(string fno)
        {
            InitializeComponent();
            FBrNo = fno;
        }


        public ICEntrustDetail(string fno,string _ID)
        {
            InitializeComponent();
            FBrNo = fno;
            this.ID = _ID;
        }

        private void ICEntrustDetail_Load(object sender, EventArgs e)
        {
            if (FBrNo == null || FBrNo == "")
            {
            }
            else
            {
                DataGridStyle();
                BinData();
            }
        }
        //获取数据
        private void BinData()
        {
            DataSet ds = bjs.GetICEntrustEntry(FBrNo);
            DataTable dt = new DataTable();

            dt = ds.Tables[0];
            //if (dt.Rows.Count <= 0)
            //{
            //    return;
            //}
            //else
            //{
                dataGrid1.DataSource = dt;
            //}
        }
        //数据窗口样式
        public void DataGridStyle()
        {
            DataGridTableStyle dts = new DataGridTableStyle();
            dataGrid1.TableStyles.Clear();
            dts.MappingName = "ICEntrustDetail";

            DataGridTextBoxColumn cl1 = new DataGridTextBoxColumn();
            cl1.HeaderText = "流水号";
            cl1.MappingName = "FEntryNo";
            cl1.Width = 40;
            dts.GridColumnStyles.Add(cl1);

            DataGridTextBoxColumn cl2 = new DataGridTextBoxColumn();
            cl2.HeaderText = "包装码";
            cl2.MappingName = "PkgsBarCode";
            cl2.Width = 80;
            dts.GridColumnStyles.Add(cl2);

            DataGridTextBoxColumn cl3 = new DataGridTextBoxColumn();
            cl3.HeaderText = "仓位码";
            cl3.MappingName = "PlaceBarCode";
            cl3.Width = 80;
            dts.GridColumnStyles.Add(cl3);

            DataGridTextBoxColumn cl4 = new DataGridTextBoxColumn();
            cl4.HeaderText = "仓位名";
            cl4.MappingName = "PlaceName";
            cl4.Width = 80;
            dts.GridColumnStyles.Add(cl4);

            dataGrid1.TableStyles.Add(dts);

        }

        private void dataGrid1_CurrentCellChanged_1(object sender, EventArgs e)
        {
            DataGrid dataGrid1 = sender as DataGrid;
            int index = ((DataGrid)sender).CurrentCell.RowNumber;
            if (((DataTable)(dataGrid1.DataSource)).Rows.Count > 0)
            {
                ((DataGrid)sender).Select(index);
            }
            FEntryNo = ((DataTable)(dataGrid1.DataSource)).Rows[index][0].ToString();
            PkgsBarCode = ((DataTable)(dataGrid1.DataSource)).Rows[index][1].ToString();
            PlaceBarCode = ((DataTable)(dataGrid1.DataSource)).Rows[index][2].ToString();
            PlaceName = ((DataTable)(dataGrid1.DataSource)).Rows[index][3].ToString();

            this.tb_FBrNo.Text = FBrNo.Trim();
            this.tb_FEntryNo.Text = FEntryNo.Trim();
            this.tb_PkgsBarCode.Text = PkgsBarCode;
            this.tb_PlaceBarCode.Text = PlaceBarCode;
            this.tb_PlaceName.Text = PlaceName;

            this.tb_PkgsBarCode.Focus();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FEntryNo = this.tb_FEntryNo.Text.Trim();
            PkgsBarCode = this.tb_PkgsBarCode.Text.Trim();
            PlaceBarCode = this.tb_PlaceBarCode.Text.Trim();

            if (FEntryNo == null || FEntryNo == "")
            {
                MessageBox.Show("请选择委外入库记录！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                return;
            }
            if (PkgsBarCode == null || PkgsBarCode == "")
            {
                MessageBox.Show("请扫描包装码！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                return;
            }
            if (PlaceBarCode == null || PlaceBarCode == "")
            {
                MessageBox.Show("请扫描仓位码！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                return;
            }

            bool lb_return = bjs.UpdateICEntrustEntry(FEntryNo, PkgsBarCode, PlaceBarCode);
            if (lb_return)
            {
                DataGridStyle();
                BinData();
            }
            else
            {
                MessageBox.Show("提交失败！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                return;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ICEntrust ice = new ICEntrust(this.ID);
            ice.Show();
            this.Hide();
        }

        private void tb_PkgsBarCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            { 
                PkgsBarCode = this.tb_PkgsBarCode.Text.Trim();

                if (PkgsBarCode == null || PkgsBarCode == "")
                {
                    MessageBox.Show("请扫描包装码！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                    return;
                }
                else
                {
                    this.button1.Focus();
                }
            }
        
        }

        private void tb_PlaceBarCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                PlaceBarCode = this.tb_PlaceBarCode.Text;
                string FNumber = "";//仓位代码
                bool lb_return = bjs.GetPlaceName(PlaceBarCode,out FNumber,out PlaceName);

                if (lb_return)
                {
                    this.tb_PlaceName.Text = PlaceName;
                    this.button1.Focus();
                }
                else
                {
                    MessageBox.Show("仓位码不存在！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                    return;
                }

            }
        }


    }
}